package com.example.socket.context;

import com.example.common.HttpServerStart;
import com.example.proxy.MethodProxy;
import com.example.socket.ServletStart;
import com.example.socket.factory.ServerModelFactory;
import com.example.socket.model.ServletModel;
import com.example.util.ApplicationContent;
import com.example.util.ClassScanUtils;

import java.util.Map;

/**
 * @Classname ServletApplication
 * @Description TODO
 * @Date 2023/4/28 14:12
 * @Created by XiongXiong
 * @Author: X.I.O
 *
 * 基于socket 套接字实现的，可以实现web短链接和长连接
 */
public class ServletApplication {

    public static void run(String[] args,Class<?> cls){
        try {
            ClassScanUtils.searchClass(cls);
            ClassScanUtils.scanClass(cls);
            ApplicationContent.start();
            //读取配置
            Map<String,String> config=ApplicationContent.getConfig();
            String port=config.get("server.port");
            Map<String, MethodProxy> methodProxy=ApplicationContent.getWebMethodProxy();
            Map<String, ServletModel> servletModel= ServerModelFactory.getServletModelByFactory(methodProxy);
            ServletStart servletStart=new ServletStart(Integer.parseInt(port));
            servletStart.addUrlServlet(servletModel);
            servletStart.start();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
